/**
 * Copyright (C)  SXC TECH  
 * All rights reserved. 
 * @author：shisongbin
 * @date：2016年5月24日 下午4:44:24 
 * @version：V1.0 
 * @Description： 
 */

package com.smart.base.tools.generator.pojo.model;

/**
 * @ClassName：ColumnMeta
 * @Description：字段结构
 * @author：shisongbin
 * @date：2016年5月24日 下午4:44:24
 */
public class ColumnMeta {
	private String columnName;

	private String ordinalPosition;

	private String isNullable;

	private String columnDefault;

	private String columnType;

	private String columnKey;

	private String extra;

	private String columnComment;

	private int columnSize; // 字段长度，将在计算属性类型时赋值

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getOrdinalPosition() {
		return ordinalPosition;
	}

	public void setOrdinalPosition(String ordinalPosition) {
		this.ordinalPosition = ordinalPosition;
	}

	public String getIsNullable() {
		return isNullable;
	}

	public void setIsNullable(String isNullable) {
		this.isNullable = isNullable;
	}

	public String getColumnDefault() {
		return columnDefault;
	}

	public void setColumnDefault(String columnDefault) {
		this.columnDefault = columnDefault;
	}

	public String getColumnType() {
		return columnType;
	}

	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}

	public String getColumnKey() {
		return columnKey;
	}

	public void setColumnKey(String columnKey) {
		this.columnKey = columnKey;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String extra) {
		this.extra = extra;
	}

	/**
	 * 
	 * @Title：getColumnComment @Description：
	 * @return String
	 */
	public String getColumnComment() {
		return columnComment.replace("\r\n", "\r\n  	*");
	}

	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}

	/**
	 * 获取属性名称
	 * 
	 * @return
	 */
	public String getPropertyName() {
		StringBuffer propertyName = new StringBuffer();
		String[] names = columnName.startsWith("fk_") ? columnName.replace("fk_", "").toLowerCase().split("_") : columnName.toLowerCase().split("_");
		propertyName.append(names[0]); // 第一个单词全小写
		for (int i = 1, len = names.length; i < len; i++) {
			propertyName.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
		}
		return propertyName.toString();
	}

	/**
	 * 获取属性类型
	 * 
	 * @return
	 */
	public String getPropertyType() {
		String type = columnType.toLowerCase();
		String propertyType = null;
		if (type.startsWith("int") || type.startsWith("smallint") || type.startsWith("tinyint")) { // int/long
			columnSize = Integer.parseInt(type.substring(type.indexOf("(") + 1, type.indexOf(")")));

			if (columnSize == 1) {
				propertyType = "Boolean";
			} else if (columnSize <= 9) {
				propertyType = "Integer";
			} else {
				propertyType = "Long";
			}

		} else if (type.startsWith("bigint")) { // long
			propertyType = "Long";
		} else if (type.startsWith("double") || type.startsWith("decimal")) { // double
			propertyType = "BigDecimal";
		} else if (type.startsWith("float")) { // float
			propertyType = "BigDecimal";
		} else if (type.startsWith("varchar") || type.startsWith("char") || type.startsWith("text") || type.startsWith("longtext")
				|| type.startsWith("mediumtext")) { // String
			propertyType = "String";
		} else if (type.startsWith("date")) { // date
			propertyType = "java.util.Date";
		} else if (type.startsWith("datetime")) { // date
			propertyType = "java.util.Date";
		} else if (type.startsWith("timestamp")) { // date
			propertyType = "java.util.Date";
		} else if (type.startsWith("time")) { // date
			propertyType = "java.sql.Time";

		} else {
			System.out.println("==类型[" + type + "]解析尚不支持==");
		}
		return propertyType;
	}

	public int getColumnSize() {
		return columnSize;
	}
}
